/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* @file:		XiaoFei_Usart2.c
* @author:		XiaoFei
* @Date:     	13-January-2025
* @Revision: 	V1
* @Project: 	XiaoFei BSP Library
*
* -------------------------------------------------------------------- */

#include "stm32f10x.h"
#include "XiaoFei_Usart2.h"
#include "stdio.h"
#include "stdarg.h"

/* 
* @function: 	Usart2_Init
*/
void Usart2_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = Usart2_Pin_TX;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(Usart2_GPIO, &GPIO_InitStructure);
    #ifdef Usart2_AddRx
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
		GPIO_InitStructure.GPIO_Pin = Usart2_Pin_RX;
		GPIO_Init(Usart2_GPIO, &GPIO_InitStructure);
	#endif
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = Usart2_BaudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    #ifdef Usart2_AddRx
	    USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
	#else 
	    USART_InitStructure.USART_Mode = USART_Mode_Tx;
	#endif
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART2, &USART_InitStructure);
    #ifdef Usart2_AddRx
		USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
		NVIC_PriorityGroupConfig(USART2_Group);
		NVIC_InitTypeDef NVIC_InitStructure;
		NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USART2_ITPre;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = USART2_ITSub;
		NVIC_Init(&NVIC_InitStructure);
	#endif
	USART_Cmd(USART2, ENABLE);
}

/* 
* @function: 	Usart2_SendByte
* @parameter:	[in] Byte					 The byte will be send.
*/
void Usart2_SendByte(unsigned char Byte)
{
	USART_SendData(USART2, Byte);
	while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}

/* 
* @function: 	Usart2_SendString
* @parameter:	[in] String					 The String will be send.
*/
void Usart2_SendString(char *String)
{
	for (int i = 0; String[i] != '\0'; i ++)
		Usart2_SendByte(String[i]);
}

/* 
* @function: 	Usart2_Printf
* @parameter:	[in] format					 The parameter will be send in Usart2_Printf.
*/
void Usart2_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg, format);
	vsprintf(String, format, arg);
	va_end(arg);
	Usart2_SendString(String);
}

#ifdef Usart2_AddRx
/* 
* @function: 	USART2_IRQHandler
* @callback:    The function will be callback when the Usart2 receive the data.
*/
void USART2_IRQHandler(void)
{
	if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
		USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
#endif


